package pack1;

import java.io.IOException;
import java.io.PrintWriter;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.swim.session.NewSkillRequestRemote;

public class NewSkillRequestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
      
    
    public NewSkillRequestServlet() {
        super();
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String url = "/WEB-INF/noauth/index.jsp";
		Object auth = request.getSession().getAttribute("authenticated");
		if(auth != null && (boolean)auth) {
			url = "/WEB-INF/auth/newskillreq.jsp";
			RequestDispatcher dispatcher = request.getRequestDispatcher(url);
			dispatcher.forward(request, response); 
		} else
			response.sendRedirect("index.jsp");
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Object auth = request.getSession().getAttribute("authenticated");
		if(auth == null || !(boolean)auth){
			response.sendRedirect("index.jsp");
			return;
		}
		
		try{		
			String description = request.getParameter("description");
			
			if(description == null) {
				RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/auth/newskillreq.jsp");
				dispatcher.forward(request, response); 
			} else {
				
				//add user to DB
				response.setContentType("text/html");

				if(description == null || description.length() <= 0) {
					request.setAttribute("title","Description too shoort");
					request.setAttribute("message","You haven't entered a description for the new skill. Go <a href=\"newskillreq.jsp\">back</a> to the form or <a href=\"index.jsp\">home</a>");
					RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/target.jsp");
					dispatcher.forward(request, response); 
					return;
				}
				try {
					InitialContext jndiContext = (InitialContext) Util.getInitialContext();
					Object ref = jndiContext.lookup("NewSkillRequestBeanJNDI");
					NewSkillRequestRemote r = (NewSkillRequestRemote) ref;

					
					r.newSkillRequest((String)request.getSession().getAttribute("authenticated-email"), description);
					
					request.setAttribute("title","Skill Requested");
					request.setAttribute("message","<p>The skill has been requested and the administrator will examine the request.</p>" +
							"<p>Click <a href=\"index.jsp\">here</a> to go back to the homepage.</p>");
					RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/target.jsp");
					dispatcher.forward(request, response); 
					
				} catch (NamingException e) {
					e.printStackTrace();
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
			PrintWriter out = response.getWriter();
			out.print("Errore inserimento richiesta!");
		}
	}
	
}
